package com.lhb.service.Impl;

import com.lhb.dao.Repository.RoleRepository;
import com.lhb.dao.RoleMapper;
import com.lhb.dao.RolePermissionMapper;
import com.lhb.pojo.BaseResp;
import com.lhb.pojo.TbPermissionVo;
import com.lhb.pojo.TbRole;
import com.lhb.pojo.rep.RolePermissionReq;
import com.lhb.pojo.rep.TbPermissionReq;
import com.lhb.pojo.resp.TbRoleResp;
import com.lhb.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.10.13 下午 17:06
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleRepository roleRepository;

    @Autowired
    RolePermissionMapper roleMapper;

    @Autowired
    RoleMapper roleuserMapper;

    @Override
    public BaseResp findAll(Integer page, Integer limit) {
        PageRequest of = PageRequest.of(page - 1, limit);
        Page<TbRole> all = roleRepository.findAll(of);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(0);
        baseResp.setMsg("success");
        baseResp.setCount(all.getTotalElements());
        baseResp.setData(all.getContent());
        return baseResp;
    }

    @Override
    public BaseResp insertRole(TbRole tbRole) {
        TbRole save = roleRepository.save(tbRole);
        BaseResp baseResp = new BaseResp();
        if (save != null) {
            baseResp.setCode(0);
            baseResp.setMsg("添加成功");
        }
        return baseResp;
    }

    @Override
    public void deleteById(Integer id) {
        roleRepository.deleteById(id);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(0);
        baseResp.setMsg("删除成功");
    }

    @Override
    public BaseResp updatePermission(RolePermissionReq req) {
        //1.删除掉中间表的角色与权限的关联关系
        roleMapper.delteByRoleId(req.getRoleid());
        //2.建立新的逻辑关系
        List<TbPermissionReq> perlist = req.getPerlist();
        //3.建立新的集合
        if (perlist.size() > 0) {
            List list = new ArrayList();
            for (TbPermissionReq tb : perlist) {
                TbPermissionVo tbPermissionVo = new TbPermissionVo();
                tbPermissionVo.setPerid(tb.getId());
                tbPermissionVo.setRoleid(req.getRoleid());
                list.add(tbPermissionVo);
            }
            roleMapper.saveAll(list);
        }
        return new BaseResp(0, "权限修改完成", null, null);
    }

    @Override
    public BaseResp findByUser(Integer userid) {
        List<TbRoleResp> all = roleuserMapper.findAll();
        List<TbRoleResp> userRole = roleuserMapper.findByUserId(userid);
        for (TbRoleResp tb : all) {
            for (TbRoleResp ur : userRole) {
                if (tb.getTitle().equals(ur.getTitle()))
                    tb.setChecked(true);
                break;
            }
        }
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(0);
        baseResp.setData(all);
        return baseResp;
    }
}
